# coding=utf-8
import asyncio

from grpclib.server import Server

from grpclib_test.proto.hello_grpc import GreeterBase
from grpclib_test.proto.hello_pb2 import HelloReply


class Greeter(GreeterBase):
    async def SayHello(self, stream):
        request = await stream.recv_message()
        message = f'Hello, {request.name}'
        await stream.send_message(HelloReply(message=message))


# 我们课程中不会使用asyncio
async def main(*, host='127.0.0.1', port=50051):
    server = Server([Greeter()])
    # Note: graceful_exit isn't supported in Windows
    await server.start(host, port)
    print(f'Serving on {host}:{port}')
    await server.wait_closed()


if __name__ == '__main__':
    asyncio.run(main())
